package link.anycar.study.kotlin.oop

sealed class Expr

data class Const(val number: Double): Expr()

data class Sum(val e1: Expr, val e2: Expr): Expr()

object NotANumber: Expr()

fun eval(expr: Expr): Double = when(expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    is NotANumber -> Double.NaN
}

fun main(args: Array<String>) {
    val e1 = Const(10.0)
    val e2 = Const(20.0)
    val e3 = Sum(e1, e2)
    println(eval(e3))
}